package com.feidee.fd.sml.algorithm.util

import org.apache.http.{Consts, HttpEntity}
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.{CloseableHttpResponse, HttpPost}
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.{CloseableHttpClient, HttpClients}
import org.apache.http.util.EntityUtils

/**
  * @Author songhaicheng
  * @Date 2019/4/28 17:15
  * @Description
  * @Reviewer
  */
object HttpUtil {

  def postWithEncryption(path: String, url: String, body: String): String = {
    /* val ak_id = "data_team"
     //分配的AccessSecret，是rsa公钥，用于加密aes密钥
     val ak_secret = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ6+UYiQtt2OYfyfBF8e12kjd6nnF0Zl1s23f7l" +
       "+eSl07xQxoxACSXyO3aq/tF7g0IiQZev4SF7ZA/oAOIYlYo0CAwEAAQ=="
     //用户自己随机产生16位长度的AES密钥
     val aes_secret = "PiDl53SMqa1Bpmxu"
     val ciphertext = SimpleAES.encrypt(body, aes_secret) //密文
     val date: String = TimeUtils.toGMTString(new Date) //时间
     val stringToSign: String = ToolUtils.MD5StringBase64(body) + "\n" + date + "\n" + path
     val signature = s"$ak_id:${ToolUtils.HMACSha1StringBase64(stringToSign, aes_secret)}:" +
       s"${RSAUtil.encrypt(aes_secret, ak_secret)}"
     var method: HttpPost = null
     try {
       method = new HttpPost(url)
       method.getParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)
       val stringEntity = new StringEntity(ciphertext, Consts.UTF_8)
       stringEntity.setContentEncoding("UTF-8")
       stringEntity.setContentType("application/json")
       method.setHeader("Date", date)
       method.setHeader("Authorization", signature)
       method.setHeader("AuthVersion", "2")
       method.setEntity(stringEntity)
       com.feidee.fdcommon.util.HttpUtil.post(method)
     } finally {
       if (method != null) method.releaseConnection()
     }*/

    //创建JSON格式实体对象
    //val list: util.ArrayList[String] = new util.ArrayList[String]
    // list.add("/user/data_team/temp/zhijie/tfmodel/ssj_wdl_emb_v1/time_sparse_202009271825");
    // list.add("/user/data_team/tmp/spark/ssj_wdl_syyyw_v5_spark_v2_202104131347");
    /*list.add("/user/data_team/tmp/spark/ssj_wdl_syyyw_staytime")
    val k8sConvertMleapParam: ConvertModelDTO = new ConvertModelDTO(list, "/user/data_team/tmp/mleap/ssj_wdl_staytime_mleap_20210428.zip")
    val data: String = JSON.toJSONString(k8sConvertMleapParam)*/


    //请求url
    //val urlPath: String = path + url



    //超时设置
    val requestConfig: RequestConfig = RequestConfig.custom.setConnectTimeout(50000).setConnectionRequestTimeout(10000).setSocketTimeout(50000).build
    //数据准备
    val httpPost: HttpPost = new HttpPost(url)
    httpPost.setConfig(requestConfig)
    httpPost.addHeader("Content-Type", "application/json")
    httpPost.setEntity(new StringEntity(body))
    //执行post请求
    val httpClient: CloseableHttpClient = HttpClients.createDefault
    val response: CloseableHttpResponse = httpClient.execute(httpPost)
    //输出请求结果
    val entity: HttpEntity = response.getEntity
    EntityUtils.toString(entity, "UTF-8")

  }

}
